﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using ZhuGe.DAL.SqlServer;
using System.Data;

namespace ZhuGe.Web.admin
{
    public partial class addblog : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (IsPostBack)
                {
                    string title = Request.Form.Get("txtTitle");
                    string txtLables = Request.Form.Get("txtLables");
                    string txtContent = Request.Form.Get("txtContent");
                    string txtCreator = Request.Form.Get("txtCreator");                    
                    string TypeID = Request.Form.Get("TypeID");

                    if (txtLables == "")
                    {
                        txtLables = "卧龙一笑";
                    }
                    if (txtCreator == "")
                    {
                        txtCreator = "孔明";
                    }

                    string sql = "insert into T_Blogs(Title,Lables,Content,CreateTime,IsDel,Creator,TypeID,Clicks) values(@Title,@Lables,@Content,getdate(),0,@Creator,@TypeID,1)";

                    SqlConnection con = new SqlConnection(SqlHelper.ConnectionString);
                    SqlCommand sqlComm = new SqlCommand(sql,con);

                    sqlComm.Parameters.Add(new SqlParameter("@Title", SqlDbType.NVarChar, 225)); //定义变量@title
                    sqlComm.Parameters["@Title"].Value = title;     //变量@title赋值

                    sqlComm.Parameters.Add(new SqlParameter("@Lables", SqlDbType.NVarChar, 225)); //定义变量@Lables
                    sqlComm.Parameters["@Lables"].Value = txtLables;     //变量@title赋值

                    sqlComm.Parameters.Add(new SqlParameter("@Content", SqlDbType.NText, 10000)); //定义变量@Content
                    sqlComm.Parameters["@Content"].Value = txtContent;     //变量@title赋值

                    sqlComm.Parameters.Add(new SqlParameter("@Creator", SqlDbType.NVarChar, 225)); //定义变量@Creator
                    sqlComm.Parameters["@Creator"].Value = txtCreator;     //变量@title赋值

                    sqlComm.Parameters.Add(new SqlParameter("@TypeID", SqlDbType.Int, 4)); //定义变量@Creater
                    sqlComm.Parameters["@TypeID"].Value = TypeID;     //变量@title赋值

                    string sqltex = sqlComm.CommandText;

                    con.Open();
                    int res = sqlComm.ExecuteNonQuery();
                    sqlComm.Dispose();
                    con.Close();

                    if (res > 0)
                    {
                        Response.Write("<script>alert('添加成功！');window.location.href='addblog.aspx';</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('添加失败1！" + sqltex + "');window.location.href='addblog.aspx';</script>");
                    }
                }
                else
                {
                    //Response.Write("<script>alert('添加失败2！');window.location.href='index.html';</script>");
                }
            }
            catch (Exception ex)
            {
                Response.Write("<script>alert('" + ex.Message + "！');window.location.href='addblog.aspx';</script>");
                throw;
            }
        }
    }
}